<template>
  <div>
    <el-dialog
      title="添加版本"
      :visible.sync="dialogFormVisible"
      width="700px"
      center
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addVersionForm"
        :inline="true"
        :model="addVersionForm"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-row type="flex" justify="space-around">
          <el-col :span="10">
            <el-form-item label="版本号" prop="verNumber">
              <el-input v-model="addVersionForm.verNumber" placeholder="请输入版本号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="需求责任人" prop="principal">
              <el-select v-model="addVersionForm.principal" multiple placeholder="需求责任人">
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" @click="onSubmit" class="submit-btn">提交</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row type="flex">
        <el-col>
          <el-table
            ref="versionTable"
            :data="verOptions"
            border
            :header-cell-style="{background:'#F9F9F9'}"
            align="center"
          >
            <el-table-column prop="verNumber" label="版本号" width="100" align="center"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="170" align="center"></el-table-column>
            <el-table-column prop="demandUserName" label="需求责任人" align="center"></el-table-column>
            <el-table-column label="操作" width="200" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  plain
                  @click="handleModify(scope.$index, scope.row)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  plain
                  @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <el-dialog
        width="500px"
        title="修改"
        :visible.sync="modifyDialogVisible"
        center
        append-to-body
        :close-on-click-modal="false"
        :show-close="false"
      >
        <el-form ref="modifyForm" :model="modifyForm" :rules="rules">
          <el-form-item label="版本号" label-width="20%" prop="verNumber">
            <el-input v-model="modifyForm.verNumber" placeholder="请输入版本号" style="width: 90%;"></el-input>
          </el-form-item>
          <el-form-item label="责任人" label-width="20%" prop="principal">
            <el-select
              v-model="modifyForm.principal"
              multiple
              placeholder="请选择需求责任人"
              style="width: 90%;"
            >
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="modifyDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmModify">确 定</el-button>
        </div>
      </el-dialog>

      <div slot="footer" class="dialog-footer">
        <el-button style="width: 120px;" @click="closeVerDialog">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addProjectVersion,
  getPrincipalList,
  getVersionList,
  modifyProjectVersion,
  deleteProjectVersion,
} from "@/service/progressApi";

import bus from "@/components/bus";

export default {
  name: "versionDialog",
  props: ["projectId", "verOptions"],
  data() {
    return {
      dialogFormVisible: false,
      modifyDialogVisible: false,
      addVersionForm: {
        verNumber: "",
        principal: [],
      },
      modifyForm: {
        verId: null,
        verNumber: "",
        principal: [],
      },
      rules: {
        verNumber: [
          { required: true, message: "请输入版本号", trigger: "blur" },
        ],
        principal: [
          { required: true, message: "请选择需求责任人", trigger: "change" },
        ],
      },
      options: [],
    };
  },
  watch: {
    projectId(n, o) {
      this.projectId = n;
      console.log("弹框", this.projectId);
    },
  },
  created() {
    this.getPrincipalData();
  },
  methods: {
    closeVerDialog() {
      this.$refs.addVersionForm.resetFields();
      this.dialogFormVisible = false;
    },
    //提交添加项目版本
    onSubmit() {
      this.$refs.addVersionForm.validate((valid) => {
        if (valid) {
          let addPrjVer = {
            projectId: Number(this.projectId),
            verNumber: this.addVersionForm.verNumber,
            demandUserId: this.addVersionForm.principal.join(","),
          };
          addProjectVersion(addPrjVer).then((res) => {
            this.$message.success("添加成功~");
            this.$emit("updateTable");
          });
          this.$refs.addVersionForm.resetFields();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //修改项目版本
    handleModify(index, row) {
      // console.log(row);
      this.modifyDialogVisible = true;
      this.modifyForm.verId = row.id;
      this.modifyForm.verNumber = row.verNumber;
      this.modifyForm.principal = row.demandUserId
        .split(",")
        .map((elem) => Number(elem));
      // this.modifyForm.principal = row.demandUserName.split(",") || [];
    },
    confirmModify() {
      this.$refs.modifyForm.validate((valid) => {
        if (valid) {
          let modPrjVer = {
            id: this.modifyForm.verId,
            verNumber: this.modifyForm.verNumber,
            demandUserId: this.modifyForm.principal.join(","),
          };
          // console.log(modPrjVer);

          modifyProjectVersion(modPrjVer).then((res) => {
            this.$message.success("修改成功~");
            this.$emit("updateTable");
          });
          this.modifyDialogVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除项目版本
    handleDelete(index, row) {
      // console.log(index, row);
      deleteProjectVersion(row.id).then((res) => {
        if (res.code === 0) {
          this.$message.success("删除成功~");
          this.$emit("updateTable");
        } else {
          this.$message.info(res.message);
        }
      });
    },
    //获取需求责任人下拉列表
    getPrincipalData() {
      getPrincipalList().then((res) => {
        if (res.data.length > 0) {
          this.options = res.data;
        }
      });
    },
  },
};
</script>

<style scoped>
.submit-btn {
  position: relative;
  left: -14px;
}
</style>